package mvc_generator.generator.car.jsf;

import com.google.common.base.Objects;
import mvc_generator.generator.MVCGeneratorGeneratorUtils;
import mvc_generator.generator.car.jsf.ExpressionGenerator;
import mvc_generator.generator.car.jsf.JSFConfigConstantes;
import mvc_generator.mVCGenerator.BooleanType;
import mvc_generator.mVCGenerator.Condition;
import mvc_generator.mVCGenerator.Config;
import mvc_generator.mVCGenerator.Controller;
import mvc_generator.mVCGenerator.DateType;
import mvc_generator.mVCGenerator.Declaration;
import mvc_generator.mVCGenerator.DoubleType;
import mvc_generator.mVCGenerator.Expression;
import mvc_generator.mVCGenerator.Form;
import mvc_generator.mVCGenerator.FormValidation;
import mvc_generator.mVCGenerator.IntegerType;
import mvc_generator.mVCGenerator.ModelMVC;
import mvc_generator.mVCGenerator.SimpleType;
import mvc_generator.mVCGenerator.StringType;
import mvc_generator.mVCGenerator.View;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.xbase.lib.StringExtensions;

@SuppressWarnings("all")
public class ControllerGenerator {
  public void generate(final Controller controller, final IFileSystemAccess fsa) {
    ModelMVC modelMVCContainer = MVCGeneratorGeneratorUtils.findModelMVCContainer(controller);
    ExpressionGenerator _expressionGenerator = new ExpressionGenerator();
    ExpressionGenerator expressionGenerator = _expressionGenerator;
    CharSequence fileContent = null;
    boolean _equals = Objects.equal(modelMVCContainer, null);
    if (_equals) {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("@GenerationError: WebSite container not found for this controller");
      fileContent = _builder;
    } else {
      StringConcatenation _builder_1 = new StringConcatenation();
      _builder_1.append("package ");
      String _replace = JSFConfigConstantes.BEANCLASSES_PACKAGE.replace("/", ".");
      _builder_1.append(_replace, "");
      _builder_1.append(";");
      _builder_1.newLineIfNotEmpty();
      _builder_1.newLine();
      _builder_1.append("import javax.faces.bean.ManagedBean;");
      _builder_1.newLine();
      _builder_1.newLine();
      _builder_1.append("@ManagedBean");
      _builder_1.newLine();
      _builder_1.append("public class ");
      String _name = controller.getName();
      String _firstUpper = StringExtensions.toFirstUpper(_name);
      _builder_1.append(_firstUpper, "");
      _builder_1.append(" {");
      _builder_1.newLineIfNotEmpty();
      _builder_1.append("\t");
      _builder_1.newLine();
      {
        EList<Declaration> _declarations = controller.getDeclarations();
        for(final Declaration declaration : _declarations) {
          _builder_1.append("\t");
          _builder_1.append("private ");
          SimpleType _type = declaration.getType();
          String _computeStringRepresentation = this.computeStringRepresentation(_type);
          _builder_1.append(_computeStringRepresentation, "	");
          _builder_1.append(" ");
          String _name_1 = declaration.getName();
          _builder_1.append(_name_1, "	");
          _builder_1.append("; ");
          _builder_1.newLineIfNotEmpty();
        }
      }
      _builder_1.append("\t");
      _builder_1.newLine();
      _builder_1.append("\t");
      _builder_1.newLine();
      {
        EList<FormValidation> _formValidations = controller.getFormValidations();
        for(final FormValidation formValidation : _formValidations) {
          _builder_1.append("\t");
          _builder_1.append("public String validate");
          View _controlledView = controller.getControlledView();
          String _name_2 = _controlledView.getName();
          String _firstUpper_1 = StringExtensions.toFirstUpper(_name_2);
          _builder_1.append(_firstUpper_1, "	");
          String _validatedFormName = formValidation.getValidatedFormName();
          String _firstUpper_2 = StringExtensions.toFirstUpper(_validatedFormName);
          _builder_1.append(_firstUpper_2, "	");
          _builder_1.append("(){");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.append("Boolean success = true;");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.append("\t");
          String _validatedFormName_1 = formValidation.getValidatedFormName();
          String _firstLower = StringExtensions.toFirstLower(_validatedFormName_1);
          _builder_1.append(_firstLower, "		");
          _builder_1.append("Error= \"\";");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.newLine();
          {
            EList<Condition> _conditions = formValidation.getConditions();
            for(final Condition condition : _conditions) {
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.append("if((");
              Expression _condValue = condition.getCondValue();
              CharSequence _generate = expressionGenerator.generate(_condValue);
              _builder_1.append(_generate, "		");
              _builder_1.append(") == false){");
              _builder_1.newLineIfNotEmpty();
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.newLine();
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.append("\t");
              String _validatedFormName_2 = formValidation.getValidatedFormName();
              String _firstLower_1 = StringExtensions.toFirstLower(_validatedFormName_2);
              _builder_1.append(_firstLower_1, "			");
              _builder_1.append("Error += \"Error: ");
              String _errorDescription = condition.getErrorDescription();
              _builder_1.append(_errorDescription, "			");
              _builder_1.append("\\n\";");
              _builder_1.newLineIfNotEmpty();
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.append("return \"");
              View _controlledView_1 = controller.getControlledView();
              String _name_3 = _controlledView_1.getName();
              _builder_1.append(_name_3, "			");
              _builder_1.append(".xhtml\";");
              _builder_1.newLineIfNotEmpty();
              _builder_1.append("\t");
              _builder_1.append("\t");
              _builder_1.append("}");
              _builder_1.newLine();
            }
          }
          _builder_1.append("\t");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.append("/* Mettez votre code de traitement. Toutes les pages ont la forme \"<yourPageName>.xhtml\" */");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.append("return \"");
          Config _config = modelMVCContainer.getConfig();
          View _indexView = _config.getIndexView();
          String _name_4 = _indexView.getName();
          _builder_1.append(_name_4, "		");
          _builder_1.append(".xhtml\";");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("}");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.newLine();
        }
      }
      _builder_1.newLine();
      _builder_1.append("\t");
      _builder_1.newLine();
      {
        EList<Declaration> _declarations_1 = controller.getDeclarations();
        for(final Declaration declaration_1 : _declarations_1) {
          _builder_1.append("\t");
          _builder_1.append("public ");
          SimpleType _type_1 = declaration_1.getType();
          String _computeStringRepresentation_1 = this.computeStringRepresentation(_type_1);
          _builder_1.append(_computeStringRepresentation_1, "	");
          _builder_1.append(" get");
          String _name_5 = declaration_1.getName();
          String _firstUpper_3 = StringExtensions.toFirstUpper(_name_5);
          _builder_1.append(_firstUpper_3, "	");
          _builder_1.append("(){ ");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.append("return ");
          String _name_6 = declaration_1.getName();
          _builder_1.append(_name_6, "		");
          _builder_1.append(";");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("} ");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.append("public void set");
          String _name_7 = declaration_1.getName();
          String _firstUpper_4 = StringExtensions.toFirstUpper(_name_7);
          _builder_1.append(_firstUpper_4, "	");
          _builder_1.append("(");
          SimpleType _type_2 = declaration_1.getType();
          String _computeStringRepresentation_2 = this.computeStringRepresentation(_type_2);
          _builder_1.append(_computeStringRepresentation_2, "	");
          _builder_1.append(" new");
          String _name_8 = declaration_1.getName();
          String _firstUpper_5 = StringExtensions.toFirstUpper(_name_8);
          _builder_1.append(_firstUpper_5, "	");
          _builder_1.append("){");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.append("this.");
          String _name_9 = declaration_1.getName();
          _builder_1.append(_name_9, "		");
          _builder_1.append("= new");
          String _name_10 = declaration_1.getName();
          String _firstUpper_6 = StringExtensions.toFirstUpper(_name_10);
          _builder_1.append(_firstUpper_6, "		");
          _builder_1.append(";");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("}");
          _builder_1.newLine();
        }
      }
      _builder_1.append("\t");
      _builder_1.newLine();
      {
        View _controlledView_2 = controller.getControlledView();
        EList<Form> _forms = _controlledView_2.getForms();
        for(final Form form : _forms) {
          _builder_1.append("\t");
          _builder_1.append("private String ");
          String _name_11 = form.getName();
          String _firstLower_2 = StringExtensions.toFirstLower(_name_11);
          _builder_1.append(_firstLower_2, "	");
          _builder_1.append("Error;");
          _builder_1.newLineIfNotEmpty();
        }
      }
      _builder_1.append("\t");
      _builder_1.newLine();
      _builder_1.append("\t");
      _builder_1.newLine();
      {
        View _controlledView_3 = controller.getControlledView();
        EList<Form> _forms_1 = _controlledView_3.getForms();
        for(final Form form_1 : _forms_1) {
          _builder_1.append("\t");
          _builder_1.append("public String get");
          String _name_12 = form_1.getName();
          String _firstUpper_7 = StringExtensions.toFirstUpper(_name_12);
          _builder_1.append(_firstUpper_7, "	");
          _builder_1.append("Error(){ ");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.append("return ");
          String _name_13 = form_1.getName();
          String _firstLower_3 = StringExtensions.toFirstLower(_name_13);
          _builder_1.append(_firstLower_3, "		");
          _builder_1.append("Error;");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("}");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.newLine();
          _builder_1.append("\t");
          _builder_1.append("public void set");
          String _name_14 = form_1.getName();
          String _firstUpper_8 = StringExtensions.toFirstUpper(_name_14);
          _builder_1.append(_firstUpper_8, "	");
          _builder_1.append("Error(String new");
          String _name_15 = form_1.getName();
          String _firstUpper_9 = StringExtensions.toFirstUpper(_name_15);
          _builder_1.append(_firstUpper_9, "	");
          _builder_1.append("Error){");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("\t");
          _builder_1.append("this.");
          String _name_16 = form_1.getName();
          String _firstLower_4 = StringExtensions.toFirstLower(_name_16);
          _builder_1.append(_firstLower_4, "		");
          _builder_1.append("Error = new");
          String _name_17 = form_1.getName();
          String _firstUpper_10 = StringExtensions.toFirstUpper(_name_17);
          _builder_1.append(_firstUpper_10, "		");
          _builder_1.append("Error;");
          _builder_1.newLineIfNotEmpty();
          _builder_1.append("\t");
          _builder_1.append("}");
          _builder_1.newLine();
        }
      }
      _builder_1.append("}");
      _builder_1.newLine();
      fileContent = _builder_1;
    }
    String _name_18 = modelMVCContainer.getName();
    String _plus = (_name_18 + "Jsf/src/beans/");
    View _controlledView_4 = controller.getControlledView();
    String _name_19 = _controlledView_4.getName();
    String _firstUpper_11 = StringExtensions.toFirstUpper(_name_19);
    String _plus_1 = (_plus + _firstUpper_11);
    String _plus_2 = (_plus_1 + "Controller.java");
    fsa.generateFile(_plus_2, fileContent);
  }
  
  private String computeStringRepresentation(final SimpleType simpleType) {
    String stringRepresentation = "";
    if ((simpleType instanceof IntegerType)) {
      stringRepresentation = "Integer";
    } else {
      if ((simpleType instanceof DoubleType)) {
        stringRepresentation = "Double";
      } else {
        if ((simpleType instanceof StringType)) {
          stringRepresentation = "String";
        } else {
          if ((simpleType instanceof BooleanType)) {
            stringRepresentation = "Boolean";
          } else {
            if ((simpleType instanceof DateType)) {
              stringRepresentation = "Date";
            } else {
              StringConcatenation _builder = new StringConcatenation();
              _builder.append("@AutoGenerate_Error: ");
              Class<? extends SimpleType> _class = simpleType.getClass();
              String _simpleName = _class.getSimpleName();
              _builder.append(_simpleName, "");
              _builder.append(" is not a handled SimpleType");
              stringRepresentation = _builder.toString();
            }
          }
        }
      }
    }
    return stringRepresentation;
  }
}
